{% extends "school/layout.html" %}

{% set page_title = translate('学生账号管理') %}

{% block content %}
<style>
    #changePhone{
        display: none;
    }
    .name_tip{
        color: red;
        display: inline-block;
        padding-left: 15px;
    }
</style>
<div class="m-content">
<form class="form-horizontal" method="POST" id="{{ form.__class__.__name__ }}">
    {{ form.hidden_tag() }}
    <fieldset class="fieldset" style="position: relative;">
        <legend>{{translate('新建学生')}}</legend>
        {% include "school/student_form_common.html" %}

    </fieldset>
    <fieldset class="fieldset">
        <legend>{{translate('账号信息')}}</legend>

        <div class="form-group">
            <label class="control-label col-sm-2">{{ form.username.label(class_="norequired") }}:</label>
            <div class="col-sm-4 input-group account-special">
                <span class="input-group-addon" id='school_code'>{{ current_user.school.code }}.</span>
                {{ form.username(placeholder=translate('用户名以字母开头,只能含有数字字母下划线,长度在2~20之间'), class_="form-control", pattern="^[a-zA-z]\w{2,20}$", required=True) }}
            </div>
            {% if form.username.errors %}
            <div class="error">{{ form.username.errors[0] }}</div>
            {% endif %}
        </div>
        <div>
            <button class="btn btn-primary" id="save">{{translate('确认')}}</button>
        </div>
    </fieldset>
</form>
</div>
{% endblock %}

{% block js %}
<script type="text/javascript">
  var childList = [];
  var adultList = [];
  var oldList = [];
  function setTips(v){
    if(v !== '-1'){
      $.post('/school/students/get_class_type',{'cid':v},function(data){
        var d = JSON.parse(data);
        if(d.code == 0){
          if(d.is_exp_class){
            $('.is-exp').hide();
          }else{
            $('.is-exp').show();
           }
         }else{
           kalert('{{translate("获取班级信息失败")}}')
         }
      })
     }else{
       $('.is-exp').show();
    }
  }
  function getClassList(){
    $.ajax({
      type: 'get',
      url: '/school/students/get_course_type',
      cache: false,
      async: false,
      success: function(data){
        var d = JSON.parse(data);
        for(var i=0;i<d.length; i++){
          if(d[i].course_type == 'course_adults'){
            adultList.push(d[i]);
          }else if(d[i].course_type == 'course_xi_yang_hong'){
            oldList.push(d[i]);
          }else{
            childList.push(d[i]);
          }
        }
        renderClassList(childList);
      }
    });
  }
  function renderClassList(classArray){
    var $select = $('select[name = "classes"]');
    $select.empty().append('<option value="-1">{{translate("暂未分配")}}</option>');
    for(var i = 0; i < classArray.length; i++){
      $select.append('<option value="'+classArray[i].ID+'">'+classArray[i].name+'</option>');
    }
  }
  $(function(){
      var countDown = 60;
      var isCounting = false;
      var t;
      var labels = $('#StudentInfoForm').find('label.control-label');
      var cv = $('select[name="classes"]')[0].value;
      var lesson_type_form = $('fieldset .form-group')[0];
      var lessonSelectAppended = false;

      getClassList();

      for(var i = 0; i < labels.length; i++){
        if(labels[i].innerText=='验证码'||labels[i].innerText=='Verification code'){
          $(labels[i]).prepend('<span class="dot-tips is-exp">*</span>')
        }else{
          $(labels[i]).prepend('<span class="dot-tips">*</span>')
        }
      }
      if(localStorage.lang == 'lang=en'){
          lesson_type_form.style.display = 'none';
      }else{
          (function(){
              var labelCount = $('label').length;
              var inputCount = $('input').length;
              var course_type = $('input[name="course_type"]:checked').val();

              if(course_type=='course_children'){
                  renderClassList(childList);
                  $('#parent_phone')[0].placeholder = '{{translate("请填写家长电话")}}';
                  $('#parent_email')[0].placeholder = '{{translate("请填写家长Email")}}';
              }else{
                  if(course_type=='course_adults'){
                      renderClassList(adultList);
                      $('#parent_phone')[0].placeholder = '{{translate("请填写手机号码")}}';
                      $('#parent_email')[0].placeholder = '{{translate("请填写Email")}}';
                  }else{
                      renderClassList(oldList);
                      $('#parent_phone')[0].placeholder = '{{translate("请填写手机号码")}}';
                      $('#parent_email')[0].placeholder = '{{translate("请填写Email")}}';
                  }
                  for(var i = 0; i < labelCount; i++){
                    if($('input[name="course_type"]:checked')[0].id !== 'course_type-0'){
                      //Hide parent name
                      if($('label')[i].htmlFor == 'parent_name'){
                        $($('label')[i]).parent().addClass('hide-item');
                        $($('label')[i]).siblings('input').val('111111');
                      }
                      //Hide parent
                      if($('label')[i].htmlFor == 'parent_phone'&&$('label')[i].id == ''){
                        $('label')[i].innerHTML = '<span class="dot-tips">*</span>{{translate("电话")}}';
                      }
                      if($('label')[i].htmlFor == 'parent_email'&&$('label')[i].id == ''){
                        $('label')[i].innerHTML = '<span class="dot-tips">*</span>Email';
                      }
                    }else{
                      //Hide parent name
                      if($('label')[i].htmlFor == 'parent_name'){
                        $($('label')[i]).parent().removeClass('hide-item')
                      }
                      //Hide parent
                      if($('label')[i].htmlFor == 'parent_phone'&&$('label')[i].id == ''){
                        $('label')[i].innerHTML = '<span class="dot-tips">*</span>{{translate("家长电话")}}';
                      }
                      if($('label')[i].htmlFor == 'parent_email'&&$('label')[i].id == ''){
                        $('label')[i].innerHTML = '<span class="dot-tips">*</span>{{translate("家长Email")}}';
                      }
                    }
                  }
              }
          })();
      }
      setTips(cv);

      $('#return').bind('click', function(e){
          e.preventDefault();
          window.location.href = "{{ url_for('school.list_students') }}";
      });

      function setTime(val){
        if(isCounting){
            val.attr('disabled','disabled');
            val.addClass('btn-disable');
            val.html('{{translate("在")}}'+countDown+'{{translate("秒后重新发送")}}');
            countDown--;
            if(countDown<0){
                isCounting = false;
                countDown = 60;
            }
        }else{
            val.removeAttr('disabled');
            val.removeClass('btn-disable');
            val.html('{{translate("发送验证码")}}');
            clearTimeout(t);
            return false;
        }
        t = setTimeout(function(){
            setTime(val);
        },1050);
      }

      $('select[name="classes"]').change(function(e){
        var v = this.value;
        setTips(v);
      });

      $('#sendVerify').click(function(e){
          e.preventDefault();
          var phone = $.trim($('#parent_phone').val());
          var $this = $(this);
          var pReg = new RegExp('^[0-9]{11}$');
          var courseType = $('input[name="course_type"]:checked').val();
          if(pReg.test(phone)){
              $.post('/school/students/check_phone',{'phone':phone,'course_type':courseType},function(data){
                var da = JSON.parse(data);
                if(da.code == 0){
                  $.post('/vcode/send_vcode',{'phone':phone},function(data){
                      var d = JSON.parse(data);
                      if(d.meta.code == 0){
                          isCounting = true;
                          setTime($this);
                      }else{
                          if(d.meta.code == 1){
                              kalert('{{translate("验证码不正确，请重新输入")}}');
                          }else if(d.meta.code == 2){
                              kalert('{{translate("发验证码失败，请稍后再试")}}');
                          }
                      }
                  });
                }else{
                  kalert('{{translate("输入的手机号已经被占用，请检查后重新输入！")}}')
                }
              })
          }else{
              kalert('{{translate("请输入正确的手机号码！")}}')
          }
      });

      $('input[name="course_type"]').change(function(){
        var labelCount = $('label').length;
        var inputCount = $('input').length;
        for(var a = 0; a < inputCount; a++){
          if($('input')[a].type == 'text'){
            $('input')[a].value = '';
          }
        }
        if(this.id == 'course_type-1'){
          renderClassList(adultList);
          $('#parent_phone')[0].placeholder = '{{translate("请填写手机号码")}}';
          $('#parent_email')[0].placeholder = '{{translate("请填写Email")}}';
        }else if(this.id == 'course_type-2'){
          renderClassList(oldList);
          $('#parent_phone')[0].placeholder = '{{translate("请填写手机号码")}}';
          $('#parent_email')[0].placeholder = '{{translate("请填写Email")}}';
        }else if(this.id == 'course_type-0'){
          renderClassList(childList);
          $('#parent_phone')[0].placeholder = '{{translate("请填写家长电话")}}';
          $('#parent_email')[0].placeholder = '{{translate("请填写家长Email")}}';
        }
        for(var i = 0; i < labelCount; i++){
          if(this.id !== 'course_type-0'){
            //Hide parent name
            if($('label')[i].htmlFor == 'parent_name'){
              $($('label')[i]).parent().css('display','none');
              $($('label')[i]).siblings('input').val('111111');
            }
            //Hide parent
            if($('label')[i].htmlFor == 'parent_phone'&&$('label')[i].id == ''){
              $('label')[i].innerHTML = '<span class="dot-tips">*</span>{{translate("电话")}}';
            }
            if($('label')[i].htmlFor == 'parent_email'&&$('label')[i].id == ''){
              $('label')[i].innerHTML = '<span class="dot-tips">*</span>Email';
            }
          }else{
            //Hide parent name
            if($('label')[i].htmlFor == 'parent_name'){
              $($('label')[i]).parent().css('display','block');
            }
            //Hide parent
            if($('label')[i].htmlFor == 'parent_phone'&&$('label')[i].id == ''){
              $('label')[i].innerHTML = '<span class="dot-tips">*</span>{{translate("家长电话")}}';
            }
            if($('label')[i].htmlFor == 'parent_email'&&$('label')[i].id == ''){
              $('label')[i].innerHTML = '<span class="dot-tips">*</span>{{translate("家长Email")}}';
            }
          }
        }
        if(!lessonSelectAppended){
          $(lesson_type_form).append('<span class="name_tip">{{translate("*课程类型一旦选择不可更改")}}</span>');
          lessonSelectAppended = true;
        }
      });
  });
</script>
{% endblock %}
